home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / fortran / toolpack.000 / toolpack / toolpack1.2 / C / foc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-04  |  9.9 KB  |  410 lines

  1. /*-------------------------------*/
  2. /*   TOOLPACK/1   Release: 1.1   */
  3. /*-------------------------------*/
  4.  
  5. #include <ctype.h>
  6. #include "define.h"
  7.  
  8. #include <sys/types.h>
  9. #include <sys/time.h>
  10. #ifdef sgi
  11. #include <time.h>
  12. #endif
  13.  
  14.  
  15. #include <sys/stat.h>
  16.  
  17. #include <sys/dir.h>
  18.  
  19. #include "globals1.h"
  20. #include "globals2.h"
  21. #include "globals.h"
  22.  
  23.  
  24.  
  25. getarg_(n, buffer, maxsiz)
  26. int *n, *buffer, *maxsiz;
  27. {
  28.  
  29.     /* Read inter process argument n into buffer (truncating to
  30.        maxsiz-1 characters if necessary) and terminate it with
  31.        an EOS marker. Return the length of the string in length
  32.        or EOF if the argument n does not exist.
  33.  
  34.        Maximum length of arguments = MAXLINE
  35.  
  36.        n = -2  return the name of the local directory (global -
  37.            lcldir) as set by ZLOCAL in the previous tool.
  38.  
  39.        n = -1  return the name of the root directory (global -
  40.            root) as set by ZSETDV in the previous tool.
  41.  
  42.        n =  0  return the name of the next tool to be scheduled
  43.            as set by ZTOOL or ZSETOL by the previous tool.
  44.  
  45.        n =  1, MAXPRAM   return the nth argument
  46.  
  47.        All strings are truncated, if necessary, to MAXSIZ-1
  48.        characters */
  49.  
  50.        int i;
  51.        char *p;
  52.  
  53.       *buffer = EOS;
  54.  
  55.        if (*n < -2 || *n > MAXPRAM) return(EOF);
  56.  
  57.        if ( (p = inparam[ (*n) + 2] ) == NULLST) return(EOF);
  58.  
  59.        i = 1;
  60.        while ( *p != EOSCH && i++ < *maxsiz) zcctoi_(p++, buffer++);
  61.        *buffer = EOS;
  62.        return (i-1);
  63.  
  64. }
  65. zgtarg_(n, buffer, maxsiz)
  66. int *n, *buffer, *maxsiz;
  67. {
  68.  
  69.     /* Read inter process argument n into buffer (truncating to
  70.        maxsiz-1 characters if necessary) and terminate it with
  71.        an EOS marker. Return the length of the string in length
  72.        or EOF if the argument n does not exist.
  73.  
  74.        Maximum length of arguments = MAXLINE
  75.  
  76.        n = -2  return the name of the local directory (global -
  77.            lcldir) as set by ZLOCAL .
  78.  
  79.        n = -1  return the name of the root directory (global -
  80.            root) as set by ZSETDV .
  81.  
  82.        n =  0  return the name of the next tool to be scheduled
  83.            as set by ZTOOL or ZSETOL by the previous tool.
  84.  
  85.        n =  1, MAXPRAM   return the nth argument
  86.  
  87.        All strings are truncated, if necessary, to MAXSIZ-1
  88.        characters */
  89.  
  90.        int i;
  91.        char *p;
  92.  
  93.        *buffer = EOS;
  94.  
  95.        if (*n < -2 || *n > MAXPRAM) return(EOF);
  96.  
  97.        if (*n <= 0)
  98.         p = outparm[ (*n) + 2];
  99.        else
  100.             p = inparam[ (*n) + 2];
  101.  
  102.        if (p == NULLST) return(EOF);
  103.  
  104.  
  105.        i = 1;
  106.        while ( *p != EOSCH && i++ < *maxsiz) zcctoi_(p++, buffer++);
  107.        *buffer = EOS;
  108.        return (i-1);
  109.  
  110. }
  111.  
  112.  
  113. zinit_()
  114. {
  115.  
  116.     /* tie startup routine */
  117.  
  118.     char charnm1[MAXPATH], *malloc();
  119.     int istname[MAXPATH], mode = READ, one = 1, istval[MAXNAME];
  120.     int fd, i, len, sign, *ptr, pmode = WRITE;
  121.     struct filinfo finf;
  122.  
  123.     /* set up other device files */
  124.     files[STDIN].access = files[STDIN].caccess = READ;
  125.     files[STDIN].chrleft = 0;
  126.     files[STDIN].bufp = files[STDIN].buffer;
  127.     files[STDOUT].access = files[STDOUT].caccess =  WRITE;
  128.     files[STDOUT].chrleft = 0;
  129.     files[STDOUT].bufp = files[STDOUT].buffer;
  130.     files[STDERR].access = files[STDERR].caccess =  WRITE;
  131.     files[STDERR].chrleft = 0;
  132.     files[STDERR].bufp = files[STDERR].buffer;
  133.  
  134.     /* set global dirp to NULLST to prevent closing non-open
  135.        directory in first call to zlocal */
  136.     dirp = (DIR*) NULLST;
  137.  
  138.     /* mark all other files as NOTOPEN */
  139.     for (i=FIRSTFD; i <= MAXFILE; files[i++].access = NOTOPEN);
  140.  
  141.     /* set all inparam (inter process comms info from last call)
  142.      and outparam (inter process comms info from  this call)
  143.      to NULLST - by default there is no argument */
  144.  
  145.     /* C arrays start from 0 not -2 */
  146.     for (i=0; i <= MAXPRAM+2; inparam[i] = outparm[i] = NULLST) i++;
  147.  
  148.     /* open the inter process communication file
  149.        Translate the name to IST characters - groan ! */
  150.     chist_(IPCFILE, ipcfile, strlen(IPCFILE));
  151.     mode = READ;
  152.  
  153.     if (( fd = open_(ipcfile, &mode)) == -1) {
  154.         /* open fails - file non-existant or wrong permission */
  155.         ipcexst = NO;
  156.         statswd = OK;
  157.         inparam[0] = malloc(strlen(LOCALDIR)+1);
  158.         strcpy(inparam[0],LOCALDIR);
  159.         inparam[1] = malloc(strlen(ROOTDIR)+1);
  160.         strcpy(inparam[1],ROOTDIR);
  161.         inparam[2] = malloc(strlen(CENAME)+1);
  162.         strcpy(inparam[2],CENAME);
  163.     }
  164.     else { /* read the inter process information */
  165.  
  166.         /* get status and turn it into an integer */
  167.         /* when shell scripts used as user interface, */
  168.         /* value of ipcexst changed to NO ...wrc */
  169. #ifdef SHELL_SCRIPTS
  170.         ipcexst = NO;
  171. #else
  172.         ipcexst = YES;
  173. #endif
  174.         zgtcmd_(istname, &fd);
  175.         sign = getwrd_(istname, &one, istval);
  176.         ptr = istval;
  177.         sign = 1;
  178.         if (*ptr == MINUS) {
  179.             sign = -1;
  180.             ptr++;
  181.         }
  182.         one = 1;
  183.         statswd = sign*ctoi_(ptr, &one);
  184.  
  185.         /* next get local directory, root directory, next tool
  186.            and the old arguments */
  187.         for (i=0; i <= MAXPRAM+2; i++) {
  188.             len = zgtcmd_(istname, &fd);
  189.             /* copy into malloced space */
  190.             if (len != 0 ) {
  191.                 inparam[i] = malloc(len+1);
  192.                 istchr_(istname, inparam[i]);
  193.             }
  194.         }
  195.         /* close the inter process communication file and return fd */
  196.         close(fd);
  197.     }
  198.  
  199.     /* copy into global names, check files, directories etc */
  200.     /* first translate characters to IST */
  201.     chist_(inparam[1], istname, strlen(inparam[1]));
  202.     zsetdv_(istname);
  203.  
  204.     /* now set up spooler file */
  205.     /*first make sure we get fd = 3 for the spooler file
  206.       which is why we need to do this before the call to zlocal
  207.       because zlocal grabs an fd to open the directory for zrddir */
  208.     chist_(SPFLNAME, istname, strlen(SPFLNAME));
  209.     fd = create_(istname, &pmode);
  210.     /* moan if not the channel we expected or defines screwed */
  211.     if (fd != STDLST) remark_(" fd != STDLST on opening spooler file",37L);
  212.     if (FIRSTFD <= DEVFD) remark_(" FIRSTFD <= DEVFD in defines ",29L);
  213.  
  214.     chist_(inparam[0], istname, strlen(inparam[0]));
  215.     zlocal_(istname);
  216.     ztool_();
  217.  
  218.     /* set global opnrec0 to 0 to prevent user access to
  219.     record zero of direct access files */
  220.     opnrec0 = 0;
  221.     files[STDLST].access = files[STDLST].caccess =  WRITE;
  222.     files[STDLST].chrleft = 0;
  223.     files[STDLST].bufp = files[STDLST].buffer;
  224.  
  225. }
  226.  
  227. zsetdv_(name)
  228. int *name;
  229. {
  230.  
  231.     /* change the current vfs root system to name. Also set the
  232.        appropriate inter-process parameter to name (global root).
  233.        This routine would normally be used only by the command
  234.        interpreter. If name is the null string the default system
  235.        is selected */
  236.     
  237.     char charnm1[MAXPATH];
  238.  
  239.     istchr_(name, charnm1);
  240.  
  241.     if (strlen(charnm1) == 0) strcpy(root, ROOTDIR);
  242.  
  243.     else {
  244.         if (isadir(charnm1) == NO) return(ERR);
  245.         strcpy(root, charnm1);
  246.     }
  247.  
  248.     return(NOERR);
  249. }
  250.  
  251. zstats_(status)
  252. int *status;
  253. {
  254.  
  255.     /* Recover the value of the inter-process status word */
  256.     
  257.     /* set to 'kill' by the IST command interpreter prior
  258.        to tool invocation and is only reset during a ZQUIT
  259.        or ZEXIT operation */
  260.  
  261.     *status = statswd;
  262.  
  263.         return(statswd);
  264.  
  265. }
  266.  
  267. zptarg_(n, buffer)
  268. int *n, *buffer;
  269. {
  270.  
  271.     /* Set the inter process argument n to the contents of buffer.
  272.     This allows a tool to pass arguments back to its operating
  273.     environment. This routine is also used by the command interprter
  274.     to pass information to the tool being scheduled. buffer may
  275.     contain at most MAXLINE charcaters */
  276.  
  277.     char *p, *malloc(), charnm1[MAXPATH];
  278.     int len;
  279.  
  280.     if (*n >= 1 && *n <= MAXPRAM) {
  281.         len = length_(buffer) + 1;
  282.         istchr_(buffer, charnm1);
  283.         p = malloc(len);
  284.         strcpy(p, charnm1);
  285.         outparm[ (*n)+2] = p;
  286.     }
  287.  
  288.     return;
  289.  
  290. }
  291.  
  292. zsetol_(name)
  293. int *name;
  294. {
  295.  
  296.     /* Set the name of the next tool to be invoked. Not normally called
  297.        by a tool as the normal return is to the IST command interpreter
  298.        which is the default condition set up by zinit */
  299.  
  300.     char charnm1[MAXBUFF];
  301.  
  302.     istchr_(name, charnm1);
  303.  
  304.     strcpy(nxttool, charnm1);
  305.  
  306.     return;
  307.  
  308. }
  309.  
  310. ztool_()
  311. {
  312.  
  313.     /* Copy the tool name from the inter-process communication file
  314.        to the nxttool parameter. This routine is used by the high level
  315.        command interpreter when processes are spawned */
  316.  
  317.     strcpy(nxttool, inparam[2]);
  318.  
  319.     return;
  320.  
  321. }
  322.  
  323.  
  324. zexit_(status)
  325. int *status;
  326. {
  327.  
  328.     /* termination (permanent or temporary) of a  command interpreter
  329.        or command executor. Either a further tool is to be executed
  330.        or shutdown is required. In both cases all open files need to
  331.        be closed, device buffers flushed and the spool file sent for
  332.        printing and removed. If a further tool is to be run an inter
  333.        process communication file is also written */
  334.  
  335.     int fderr = STDERR;
  336.     char temp[MAXPATH];
  337.  
  338.     /* see if we are in shutdown mode */
  339.     /*if (*status != OK || nxttool == "KILL" || nxttool == "kill" ||*/
  340.         if (nxttool == "KILL" || nxttool == "kill" ||
  341.         strlen(nxttool) == 0 || strlen(nxttool) > MAXPATH ) {
  342.          /*remark_("[TIE: Terminated].", &fderr, 18L);*/
  343.          remark_("[TIE: Terminated].", 18L);
  344.          /* remove the inter process communication file */
  345.          remove_(ipcfile);
  346.  
  347.          /* clean up and exit */
  348.          xexit();
  349.          exit(0);
  350.     }
  351.  
  352.     else {
  353.         /* output ipc file, tidy up and execute next tool */
  354.         writipc(status);
  355.         xexit();
  356.         execl(strcat(strcpy(temp,TOOLPRX),nxttool), nxttool, (char*) 0);
  357.         remark_("EXECL FAILS attempting to run.", 30L);
  358.         zmess_(nxttool, &fderr, strlen(nxttool));
  359.         exit(1);
  360.     }
  361.  
  362. }
  363.  
  364. zquit_(status)
  365. int *status;
  366. {
  367.  
  368.     /* termination routine for either a command interpreter scheduled
  369.        tool (ipcexst == YES) or standalone tool (ipcexst == NO) */
  370.  
  371.     char temp[MAXPATH];
  372.     int fderr = STDERR, i, width = 4;
  373.  
  374.     if (ipcexst == YES) {
  375.         /* command interpreter scheduled tool */
  376.         writipc(status);
  377.         xexit();
  378.  
  379.         /* schedule the command interpreter */
  380.         execl(strcat(strcpy(temp,TOOLPRX), CENAME), CENAME, (char*) 0);
  381.         remark_("EXECL FAILS attempting to schedule command interpreter"
  382.                , 54L);
  383.         exit(1);
  384.     }
  385.  
  386.     else {
  387.         /* stand alone mode - output next tool and parameters */
  388. #ifdef SHELL_SCRIPTS
  389.         /* remove inter-process file  */
  390.         remove_(ipcfile);
  391.         /* write the termination status to an information file */
  392.         outinfo(status);
  393. #endif
  394.         if (*status != OK) outcmps(status);
  395.         xexit();
  396.         if (strlen(nxttool)!=0 && strcmp(nxttool,CENAME)!=0) {
  397.  
  398.             for (i=1; i<=MAXPRAM; i++){
  399.                 if (outparm[i] != NULLST) {
  400.                     zchout_("Parameter .", &fderr, 11L);
  401.                     zptint_(&i, &width, &fderr);
  402.                     zchout_(" = ", &fderr, 3L);
  403.                     zmess_(outparm[i], &fderr, strlen(outparm[i]));
  404.                 }
  405.             }
  406.         }
  407.         exit(0);
  408.     }
  409. }
  410.